<?php

namespace App\Http\Controllers\Api\v1;

use App\Repositories\Contracts\CategoryRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\CategoryIndexRequest;

class CategoryController extends Controller
{
    protected $category;

    public function __construct(CategoryRepository $categoryRepository)
    {
        $this->category = $categoryRepository;
    }

    public function index(Request $request, CategoryIndexRequest $categoryIndexRequest)
    {
        $category = $this->category->index();

        return self::success($this->display($category));
    }

    private function display($category, $multi = true)
    {
        $data = [];
        if ($multi) {
            foreach ($category as $item) {
                $data[] = $this->category->format($item);
            }
        } else {
            $data = $this->category->format($category);
        }

        return $data;
    }
}
